<?

include '../libraries/support.php';
function print_all_entries()
{
	$sql="SELECT * FROM `blog` WHERE 1 ORDER BY `blog`.`ID` ASC";
	$result = mysql_query($sql) or die (mysql_error());
	for ($i=0;$i<mysql_num_rows($result);$i++)
	{
		$row = mysql_fetch_assoc($result); 
		$ID = $row['ID'];
		if (! empty($row['date']))
				$Date = format_and_extend_date($row['date']);
		else
				$Date = 'Planned';
		$Summary = $row['summary'];
		$Desc = $row['content'];
		$active = $row['active'];
		?>
		<B>Summary:</B><A href="blogadmin.php?id=<? echo $ID; ?>&action=edit"> <? echo $Summary; ?></A><BR>
		<B>Description:</B> <? echo $Desc; ?><BR>
		<B>Date:</B> <? echo $Date; ?><BR>
		<? if ($active == 1)
			{
			echo "entry is active. <A href='blogadmin.php?action=make_inactive&id=" . $ID . "'>Make inactive</A><BR>";
			}
		else 
			{
			echo "entry is hidden. <A href='blogadmin.php?action=make_active&id=" . $ID . "'>Make active</A><BR>";
			}
		?><BR /><BR />
		<?
	}
	mysql_free_result($result);
}
function make_active($id)
{
$sql="UPDATE `blog` SET `active`=1 WHERE `id`= $id ";
mysql_query($sql) or die (mysql_error());
}
function make_inactive($id)
{
$sql="UPDATE `blog` SET `active`=0 WHERE `id`= $id ";
mysql_query($sql) or die (mysql_error());
}
function show_entry_for_edit($id)
{
	$sql="SELECT * FROM `blog` WHERE `id`=$id ";
	$result = mysql_query($sql) or die (mysql_error());
	if (mysql_num_rows($result) < 1)
		die ("No entry with ID=$id");
	?>
	<form name="edit_event" action="blogadmin.php?action=save" method="post">
	<table name="Edit entry">
	<?
	$entry_data=mysql_fetch_assoc($result);
	echo "<tr><td>ID: </td><td>" . $entry_data['ID'] . "</td></tr>";
	echo '<input type="hidden" name="id" value="' . $entry_data['ID'] . '">';
	echo '<tr><td>Date (yyyy-mm-dd): </td><td><input type="text" name="date" value="' . $entry_data['date'] . '"> </td></tr>';
	echo '<tr><td>entry summary: </td><td><input type="text" name="summary" value="' . $entry_data['summary'] . '" > </td></tr>';
	echo '<tr><td>entry description: </td><td><textarea name="description" cols="60" rows="10" >' . $entry_data['content'] . '</textarea> </td></tr>';
	echo '<tr><td><input type="submit" value="Save changes"> </td></tr>';
	mysql_free_result($result);
	?>
	</table>
	</form>
	<?
}
function show_entry_empty_form()
{
	?>

	<form name="edit_event" action="blogadmin.php?action=insert" method="post">
	<table name="Input entry">
	<tr><td>Date (day month year): </td><td><input type="text" name="date"> </td></tr>
	<tr><td>Entry summary: </td><td><input type="text" name="summary"> </td></tr>
	<tr><td>Entry description: </td><td><textarea name="description" cols="60" rows="10"></textarea></td></tr>
	<tr><td><input type="submit" value="Save new entry"></td></tr>
	<table>
	</form>
	<?
}
function save_entry($id,$date,$summary,$description)
{
	$summary = ucfirst($summary);
	$description = ucfirst($description);
	$sql="UPDATE `blog` SET `date`='$date' , `summary` = '$summary' , `content` = '$description' WHERE `ID` = $id";
	mysql_query($sql) or die ("entry update error, $sql");
	if (($date=="0000-00-00") or ($date==""))
	{
		$sql=" UPDATE `blog` SET `date`=NULL WHERE `ID`=$id";
		mysql_query($sql) or die("Date cannot be NULL");
	}
}
function insert_entry($date,$summary,$description)
{
	$summary = ucfirst($summary);
	$description = ucfirst($description);
	if (($date=="0000-00-00") or ($date==""))
	{
		$sql="INSERT INTO `blog` (`summary`,`content`) VALUES ('$summary','$description')";
	}
	else
		$sql="INSERT INTO `blog` (`date`,`summary`,`content`) VALUES ('$date','$summary','$description')";
	mysql_query($sql) or die ("entry insert error");
}
function print_admin_navigator()
{
	?>
	<div id="navigator">
	<h3>Admin navigation</h3>
	<P><A href="blogadmin.php">List blog entries</A></P>
	<P><A href="blogadmin.php?action=add_new">Add entry</A></P>
	<P><A href="index.php">Back to main admin panel</A></P>
	<P><A href="../auth/logout.php">Log out</A></P>
	</div>
	<?
}
function validate_login($login)
{
	$sql = "SELECT `name` FROM `passwords` WHERE `name` = '$login'";
	$result = mysql_query($sql) or die ("Cannot ask for login");
	if (mysql_num_rows($result) == 0)
		$login_details = 'any';
	else
		$login_details = mysql_result($result,0);
	return $login_details;
}
function get_password($login)
{
	$sql = "SELECT `password` FROM `passwords` WHERE `name` = '$login'";
	$result = mysql_query($sql) or die ("Cannot ask for password");
	if (mysql_num_rows($result)==0)
		$password_details = md5("ljsafksajdfiewjfi092984932jlkdsjflkdsj9i923923jkjkdsjf");
	else
		$password_details = mysql_result($result,0);
	return $password_details;
}
function log_access()
{
	$login = $_SESSION["login"];
	$ip = $_SERVER['REMOTE_ADDR'];
	$date = date("Y-m-d H:i:s", strtotime("+8 hours"));
	$sql = "INSERT INTO `access_trial` (`name`,`date`,`ip`) VALUES('$login', '$date','$ip')";
	mysql_query($sql) or die ("Cannot put access data");
}
function default_list()
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
	<HTML>
	<HEAD>
	<TITLE><? echo "$site_title";?></TITLE>
	<script type="text/javascript"></script>
	
		<style type="text/css" media="all">
			@import "../bbogd.css";
		</style>

	</HEAD>
	<BODY>
	<?
	print_admin_navigator();
	?>
<div id="container">
<div id="main">
	<?
	print_all_entries();	
	?>
</div>
</div>
</body>
<?
}
function string_to_date_translator($old_date)
{
	if ($old_date == "" or $old_date == "0000-00-00")
	{
		return "";
	}
	else
	{
		list($inday,$inmonth,$inyear)=explode(" ",$old_date);
		$inmonth = strtoupper($inmonth);
		$inmonth = substr($inmonth,0,3);
		switch($inmonth){
		case "JAN":
			$dmonth="01";
			break;
		case "FEB":
			$dmonth="02";
			break;
		case "MAR":
			$dmonth="03";
			break;
		case "APR":
			$dmonth="04";
			break;
		case "MAY":
			$dmonth="05";
			break;
		case "JUN":
			$dmonth="06";
			break;
		case "JUL":
			$dmonth="07";
			break;
		case "AUG":
			$dmonth="08";
			break;
		case "SEP":
			$dmonth="09";
			break;
		case "OCT":
			$dmonth="10";
			break;
		case "NOV":
			$dmonth="11";
			break;
		case "DEC":
			$dmonth="12";
			break;
		default:
			die("Improper date format" . $inmonth . ";" . $old_dat);
		break;
		}
		$r_date=$inyear . "-" . $dmonth . "-" . $inday;
		return $r_date;
	}
}
?>
